package b;


public class 岛屿个数 {

    static char[][] g= {
            {'1','1','1','1','0'},
            {'1','1','0','1','0'},
            {'1','1','0','0','1'},
            {'0','0','0','0','1'}
    };

    //im jn
    static int M=g.length, N=g[0].length;
    public static void main(String[] args) {
        int count =0;
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                if(g[i][j]=='1'){
                    dfs_buhuisu(i,j);
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    static int[][] fx={
             {-1,0},
         {0,-1},  {0,1},
              {1,0}
    };

    private static void dfs_buhuisu(int i, int j) {

        for (int k = 0; k < 4; k++) {
            int ni= i+ fx[k][0], nj=j+ fx[k][1];
            if(ni<0||ni>= M ||nj<0||nj>= N ||g[ni][nj]=='0')continue;

            //只+不-
            g[i][j]='0'; dfs_buhuisu(ni,nj);
        }
    }

}
